package one.stand.controller;

import com.jmp.base.AddressApi;
import com.jmp.base.dto.*;
import one.stand.dto.ConvertUtils;
import one.stand.model.Address;
import one.stand.model.ResultModel;
import one.stand.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @author cjr
 */
@RestController
public class AddressController implements AddressApi {

    @Autowired
    private AddressService addressService;

    @Override
    public ResultModel<AddressDefaultVo> default1(@RequestBody AddressDefaultDto request) {
        Address address = addressService.getDefault(request.getUserId());
        AddressDefaultVo vo = null;
        if (Objects.nonNull(address)) {
            vo = AddressDefaultVo.builder()
                    .addressId(address.getAddressId())
                    .locationAddress(address.getLocationAddress())
                    .detailedAddress(address.getDetailedAddress())
                    .receiveName(address.getReceiveName())
                    .receivePhone(address.getReceivePhone())
                    .cityName(address.getCityName())
                    .latitude(address.getLatitude())
                    .longitude(address.getLongitude())
                    .build();
        }
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel<AddressListVo> list(@RequestBody AddressListDto request) {

        List<Address> addressModels = addressService.list(request.getUserId());
        List<AddressListVo.Address> addressList = ConvertUtils.list(addressModels, addressModel -> {
            return AddressListVo.Address.builder()
                    .addressId(addressModel.getAddressId())
                    .locationAddress(addressModel.getLocationAddress())
                    .detailedAddress(addressModel.getDetailedAddress())
                    .isDefault(addressModel.getIsDefault())
                    .receiveName(addressModel.getReceiveName())
                    .receivePhone(addressModel.getReceivePhone())
                    .cityName(addressModel.getCityName())
                    .latitude(addressModel.getLatitude())
                    .longitude(addressModel.getLongitude())
                    .build();
        });

        AddressListVo vo = AddressListVo.builder()
                .addressList(addressList)
                .build();
        return ResultModel.success(vo);
    }

    @Override
    public ResultModel save(@RequestBody AddressSaveDto request) {

        Address addressModel = new Address();
        addressModel.setAddressId(request.getAddressId());
        addressModel.setUserId(request.getUserId());
        addressModel.setLocationAddress(request.getLocationAddress());
        addressModel.setDetailedAddress(request.getDetailedAddress());
        addressModel.setIsDefault(request.getIsDefault());
        addressModel.setReceiveName(request.getReceiveName());
        addressModel.setReceivePhone(request.getReceivePhone());
        addressModel.setCityName(request.getCityName());
        addressModel.setLatitude(request.getLatitude());
        addressModel.setLongitude(request.getLongitude());
        addressService.save(addressModel);

        return ResultModel.success();
    }

    @Override
    public ResultModel remove(@RequestBody AddressRemoveDto request) {

        addressService.remove(request.getAddressId());

        return ResultModel.success();
    }

    @Override
    public ResultModel defaultSet(@RequestBody AddressDefaultSetDto request) {

        addressService.setDefault(request.getAddressId());

        return ResultModel.success();
    }
}
